package com.example.restatr.Atcivity.CitySubway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.example.restatr.Adapter.NewsListAdapter;
import com.example.restatr.Atcivity.CitySubway.Bean.WeatherBean;
import com.example.restatr.Bean.NewsBean;
import com.example.restatr.Atcivity.ContentAt;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.example.restatr.Atcivity.NewsAt;
import com.example.restatr.Atcivity.Main.FeedBackAt;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class CitySubwayAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private Banner dtBanner;
    private Timer timer = new Timer();
    private TextView title;
    private TextView time;
    private LinearLayout gd;
    private List<String> titles = new ArrayList<>();
    private List<String> times = new ArrayList<>();
    private List<String> content = new ArrayList<>();
    private ViewFlipper VF;
    private int vfPos = 0;
    private RecyclerView dtXw;
    private ImageView searchIcon;
    private TextView Text2;
    private LinearLayout weatherLl;
    private ArrayList<NewsBean.RowsBean> object = new ArrayList<>();
    private List<String> dtsmList = new ArrayList<>();
    private LinearLayout dtMore;
    private NewsListAdapter dtXwListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.csdt_at_layout);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        dtBanner = findViewById(R.id.banner);
        title = findViewById(R.id.title);
        time = findViewById(R.id.time);
        gd = findViewById(R.id.gd);
        VF = findViewById(R.id.VF);
        dtXw = findViewById(R.id.dt_xw);
        dtMore = findViewById(R.id.dt_more);
        searchIcon = findViewById(R.id.searchIcon);
        Text2 = findViewById(R.id.Text2);
        weatherLl = findViewById(R.id.weatherLl);

        searchIcon.setVisibility(View.VISIBLE);
        Text2.setVisibility(View.VISIBLE);

        //地铁新闻更多点击事件
        dtMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CitySubwayAt.this, NewsAt.class);
                intent.putExtra("Link", "/prod-api/api/metro/press/category/list");
                intent.putExtra("info", "dt");
                intent.putExtra("NewsLink", "/prod-api/api/metro/press/press/list?type=");
                intent.putExtra("LikeLink", "/prod-api/api/metro/press/press/like/");
                startActivity(intent);
            }
        });
        //公告更多点击事件
        gd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CitySubwayAt.this, LostAndFoundAt.class));
            }
        });
        //天气信息点击事件
        weatherLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CitySubwayAt.this, WeatherAt.class));
            }
        });
        //公告点击事件
        VF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CitySubwayAt.this, ContentAt.class);
                intent.putExtra("content", content.get(vfPos));
                intent.putExtra("title", titles.get(vfPos));
                intent.putExtra("type", "default");
                startActivity(intent);
            }
        });

        Tools.SetBanner("/prod-api/api/metro/rotation/list", "", "rows", "advImg", dtBanner, this);
        Tools.SetBarTitle(tb, barTitle, "城市地铁", search, this, false);

        //通知
        new Thread(() -> {
            for (int i = 1; i < 4; i++) {
                dtsmList.add(Tools.Get("/prod-api/api/metro/statement?type=" + i, ""));
            }
            final String get = Tools.Get("/prod-api/api/metro/notice/list", "");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //地铁声明
                        if (dtsmList.size() > 0) {
                            for (int i = 0; i < dtsmList.size(); i++) {
                                JSONObject xzJx = new JSONObject(dtsmList.get(i));
                                JSONObject data = xzJx.getJSONObject("data");
                                titles.add(data.getString("title"));
                                times.add(data.getString("createTime"));
                                content.add(data.getString("content"));
                            }
                        }
                        //地铁公告
                        if (get != null) {
                            JSONObject jx = new JSONObject(get);
                            JSONArray rows = jx.getJSONArray("rows");
                            title.setText(rows.getJSONObject(0).getString("title"));
                            time.setText(rows.getJSONObject(0).getString("createTime"));
                            for (int i = 0; i < rows.length(); i++) {
                                JSONObject all = rows.getJSONObject(i);
                                titles.add(all.getString("title"));
                                times.add(all.getString("createTime"));
                                content.add(all.getString("content"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }).start();
        //地铁新闻
        new Thread(() -> {
            final String get = Tools.Get("/prod-api/api/metro/press/press/list?type=6", "");
            if (get != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        NewsBean newsBean = new Gson().fromJson(get, NewsBean.class);
                        object.addAll(newsBean.getRows());
                        dtXwListAdapter = new NewsListAdapter(CitySubwayAt.this, object, "/prod-api/api/metro/press/press/like/");
                        dtXwListAdapter.setXwListAdapter(dtXwListAdapter);
                        dtXw.setAdapter(dtXwListAdapter);
                    }
                });
            }
        }).start();
        //获取今日天气
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/common/weather/today", "");
            WeatherBean weatherBean = new Gson().fromJson(get, WeatherBean.class);
            runOnUiThread(() -> {
                setWeather(weatherBean.getData().getWeather());
                Text2.setText(weatherBean.getData().getTemperature()+"℃");
            });
        }).start();

        timer.schedule(timerTask, 2000, 2000);

    }

    private void setWeather(String weather) {
        switch (weather) {
            case "晴":
                searchIcon.setImageResource(R.drawable.ic_round_wb_sunny_24);
                break;
        }
    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (times != null) {
                        vfPos++;
                        if (vfPos == times.size()) {
                            vfPos = 0;
                        }
                        if (times.size() > 0) {
                            time.setText(times.get(vfPos));
                            title.setText(titles.get(vfPos));
                            VF.startFlipping();
                            VF.stopFlipping();
                        }
                    }

                }
            });
        }
    };

    public void cardClick(View view) {
        switch (view.getId()) {
            case R.id.ddk:
                startActivity(new Intent(CitySubwayAt.this, MetroCardAt.class));
                break;
            case R.id.xl:
                startActivity(new Intent(CitySubwayAt.this, MetroLineAt.class));
                break;
            case R.id.yjfk:
                startActivity(new Intent(CitySubwayAt.this, FeedBackAt.class));
                break;
            case R.id.swzl:
                startActivity(new Intent(CitySubwayAt.this, LostAndFoundAt.class));
                break;
        }
    }

}